this.name                                        = "OSEhiredGuns_system";
this.author                                        = "Thargoid, modified by Lestradae";
this.copyright                                = "Creative Commons: attribution, non-commercial, sharealike.";
this.description                                = "Set up the player escorts when bought";
this.version                                = "1.0";

this.startUp = this.reset = function()
        {
        missionVariables.OSEhiredGuns_chance = Math.random() + (system.government * 0.05);
        }

this.shipWillDockWithStation = function(station)
        {
        if(!station.isMainStation)
                {
                return;
                }

        if(system.ID == missionVariables.OSEhiredGuns_launchSystem) // if we haven't gone anywhere
                {
                return;
                }

        var escortArray = system.shipsWithRole("OSEhiredGuns_escort");
        if(escortArray.length > 0)
                {
                for (var i = 0; i < escortArray.length; i++)
                        {
                        escortArray[i].remove();
                        }
                }
        }

this.shipWillEnterWitchspace = function()
        {
        var escortArray = system.shipsWithRole("OSEhiredGuns_escort");
        if(escortArray.length > 0)
                {
                for (var i = 0; i < escortArray.length; i++)
                        {
                        escortArray[i].remove()
                        missionVariables.OSEhiredGuns_count += 1;
                        }
                }
        }

this.alertConditionChanged = function()
        {
        if(player.alertCondition == 3 && player.alertHostiles && missionVariables.OSEhiredGuns_purchased)
                {
                var escortArray = system.shipsWithRole("OSEhiredGuns_escort", player.ship, 35000);
                if(escortArray.length > 0)
                        {
                        for (var i = 0; i < escortArray.length; i++)
                                {
                                escortArray[i].AIState = ("FIND_PLAYER_HOSTILES");
                                }
                        }
                }
        }

this.shipExitedWitchspace = function()
        {
        if(missionVariables.OSEhiredGuns_count > 0)
                {
                system.legacy_addShipsWithinRadius(missionVariables.OSEhiredGuns_role, missionVariables.OSEhiredGuns_count, "abs", player.ship.position, 20000);
                }
        missionVariables.OSEhiredGuns_chance = Math.random() + (system.government * 0.05);
        missionVariables.OSEhiredGuns_launchSystem = null; // just in case we jump out and back to the same system, so the escorts leave.
        }

this.playerEnteredNewGalaxy = function(galaxyNumber)
        {
        var escortArray = system.shipsWithRole("OSEhiredGuns_escort");
        if(escortArray.length > 0)
                {
                for (var i = 0; i < escortArray.length; i++)
                        {
                        escortArray[i].remove();
                        }
                }
        missionVariables.OSEhiredGuns_count = 0;
        missionVariables.OSEhiredGuns_role = null;
        missionVariables.OSEhiredGuns_purchased = null;
        missionVariables.OSEhiredGuns_launchSystem = null;
        }

this.shipWillLaunchFromStation = function(station)
        {
        if(station.isMainStation && system.countShipsWithRole("OSEhiredGuns_escort") == 0 && missionVariables.OSEhiredGuns_count > 0 && missionVariables.OSEhiredGuns_role)
                {
                system.legacy_addShipsWithinRadius(missionVariables.OSEhiredGuns_role, missionVariables.OSEhiredGuns_count, "abs", system.mainStation.position, 20000);
                var escortArray = system.shipsWithRole("OSEhiredGuns_escort");
                if(escortArray.length > 0)
                        {
                        escortArray[0].AIState = "INITIALISE";
                        }
                this.removeEq();
                }
        }

this.shipEnteredStationAegis = function(station)
        {
        if(!station.isMainStation) // Only main stations have aegis, but just to be safe
                {
                return;
                }

        if(system.ID == missionVariables.OSEhiredGuns_launchSystem) // if we haven't gone anywhere
                {
                return;
                }

        var escortArray = system.shipsWithRole("OSEhiredGuns_escort");
        if(escortArray.length > 0)
                {
                for (var i = 0; i < escortArray.length; i++)
                        {
                        escortArray[i].AIState = ("MISSION_ACCOMPLISHED");
                        }
                }

        missionVariables.OSEhiredGuns_count = 0;
        missionVariables.OSEhiredGuns_role = null;
        missionVariables.OSEhiredGuns_purchased = null;
        }

this.playerBoughtEquipment = function(equipment)
        {
        switch(equipment)
                {
                case "EQ_OSE_HIREDGUN_1":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort1";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_1");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_2":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort2";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_2");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_3":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort3";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_3");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_4":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort4";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_4");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_5":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort5";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_5");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_6":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort6";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_6");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_7":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort7";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_7");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_8":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort8";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_8");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_9":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort9";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_9");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_10":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort10";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_10");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_11":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort11";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_11");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_12":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort12";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_12");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_13":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort13";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_13");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_14":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort14";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_14");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_15":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort15";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_15");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_16":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort16";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_16");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_17":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort17";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_17");
                        break;
                        }

                case "EQ_OSE_HIREDGUN_18":
                        {
                        missionVariables.OSEhiredGuns_role = "OSEhiredGuns_escort18";
                        this.admin();
                        player.ship.removeEquipment("EQ_OSE_HIREDGUN_18");
                        break;
                        }
                }
        }

this.guiScreenChanged = function(newScreen, oldScreen)
        {
        if(oldScreen == "GUI_SCREEN_EQUIP_SHIP")
                {
                this.removeEq();
                }
        }

this.removeEq = function()
        {
        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_1"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_1");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_2"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_2");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_3"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_3");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_4"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_4");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_5"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_5");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_6"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_6");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_7"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_7");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_8"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_8");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_9"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_9");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_10"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_10");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_11"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_11");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_12"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_12");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_13"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_13");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_14"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_14");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_15"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_15");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_16"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_16");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_17"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_17");
                return;
                }

        if(player.ship.hasEquipment("EQ_OSE_HIREDGUN_18"))
                {
                player.ship.removeEquipment("EQ_OSE_HIREDGUN_18");
                return;
                }
        }


this.admin = function()
        {
        missionVariables.OSEhiredGuns_count = 2;
        missionVariables.OSEhiredGuns_launchSystem = system.ID;
        missionVariables.OSEhiredGuns_purchased = true;
        }